﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using TTMain.Settings;

namespace Thurn_und_Taxis
{
    /// <summary>
    /// Class which holds and send user settings (nickname, IP, port) to Main module
    /// </summary>
    public partial class SettingsForm : Window
    {
        private GameSettings _gameSettings;

        public SettingsForm(GameSettings gameSettings)
        {
            InitializeComponent();
            _gameSettings = gameSettings;
            
            // load last saved settings            
            txtBoxEnterUserName.Text = _gameSettings.Nickname;
            txtBoxEnterIP.Text = _gameSettings.ServerIP;
            txtBoxEnterPort.Text = _gameSettings.ServerPort.ToString();
            txtBoxLocalPort.Text = _gameSettings.LocalPort.ToString();
        }

        /// <summary>
        /// Saves the settings and closes the window
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnConnect_Click(object sender, RoutedEventArgs e)
        {
            // send user settings
            try
            {
                if (_gameSettings != null)
                {
                    _gameSettings.Nickname = txtBoxEnterUserName.Text;
                    _gameSettings.ServerIP = txtBoxEnterIP.Text;
                    _gameSettings.ServerPort = Convert.ToInt32(txtBoxEnterPort.Text.ToString());
                    _gameSettings.LocalPort = Convert.ToInt32(txtBoxLocalPort.Text.ToString());
                    _gameSettings.SaveGameSettings();
                    this.Close();
                }
            }
            // handling the case where the user enters a non-number server port
            catch(System.FormatException)
            {
                MessageBox.Show("You have entered an invalid port!");                
            }            
        }

        /// <summary>
        /// Closes the form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonBack_Click(object sender, RoutedEventArgs e)
        {
            _gameSettings = null;
            this.Close();
        }

    }
}
